﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sayeet
{
    public partial class ChatWindow : Form
    {
        AppData appData;
        TabPage clickedTab = null;

        public ChatWindow(AppData ad)
        {
            InitializeComponent();
            appData = ad;
        }

        public void addTab(ChatPane pane)
        {
            if (appData.chatWindow == null)
                appData.chatWindow = new ChatWindow(appData);
            this.Show();
            TabPage page = new TabPage();
            page.Text = pane.Name;
            pane.Dock = DockStyle.Fill;
            

            page.Controls.Add(pane);
            chatTabs.TabPages.Add(page);
        }

        private void ChatWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (TabPage tab in chatTabs.TabPages)
            {
                ChatPane pane = (ChatPane)tab.Controls[0];
                pane.chat.closeChat();
                appData.chatWindow = new ChatWindow(appData);
            }
        }

        private void chatTabs_MouseUp(object sender, MouseEventArgs e)
        {

            Point mousePosition = Control.MousePosition;
            if (e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < chatTabs.TabCount; i++)
                {
                    Rectangle r = chatTabs.GetTabRect(i);
                    if (r.Contains(e.Location))
                    {
                        tabMenu.Show(chatTabs, e.Location);
                        clickedTab = chatTabs.TabPages[i];
                        appData.debugWindow.addText(clickedTab.Text + " tab clicked");
                    }
                }
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            appData.debugWindow.addText(clickedTab.Text + " close");
            ChatPane pane = (ChatPane)clickedTab.Controls[0];
            pane.chat.closeChat();
            chatTabs.TabPages.Remove(clickedTab);
            if (chatTabs.TabCount == 0)
                appData.chatWindow.Hide();
            clickedTab = null;
        }
    }
}
